// 1.导入express
const express = require('express')
// 导入jwt的配置
const { TOKEN_KEY, TIME } = require('../JWT/config')
// 2.创建路由对象
const router = express.Router()

// 导出数据库相关配置
const { getAll, getOne, exec } = require('../db')

// 编写接口

// 获取所有用户
router.get('/', async function (req, res, next) {
  // 1. 解析req参数
  console.log(req.query, 111)
  const { page, size, keyword } = req.query

  const offset = (page - 1) * size
  let sql = ``
  if (!keyword) {
    // 2. 操作数据库
    sql = `select user_id, user_name, user_type,user_password,user_onlinestatus,user_other from gg_user limit ${offset}, ${size}`
    const data = await getAll(sql)
    sql = `select count(*) as total from gg_user`
    const { total } = await getOne(sql)

    // 3. 返回结果
    // res.status(响应状态码)
    res.send({
      code: 0,
      message: '获取分页数据成功',
      result: {
        total,
        data,
      },
    })
    return
  } else {
    sql = `select * from gg_user where user_name like '%${keyword}%' limit ${offset}, ${size}`
    const data = await getAll(sql)
    sql = `select count(*) as total from gg_user where user_name like '%${keyword}%'`
    const { total } = await getOne(sql)

    // 3. 返回结果
    // res.status(响应状态码)
    res.send({
      code: 0,
      message: '获取分页数据成功',
      result: {
        total,
        data,
      },
    })
  }
})
/**
 * 根据id获取单个用户信息
 * GET/users/:id
 */
router.get('/:user_id', async function (req, res, next) {
  //解析请求参数
  const { user_id } = req.params
  //数据库
  let sql = `select * from gg_user where user_id=${user_id}`
  const data = await getOne(sql)
  //执行sql返回结果
  res.send({
    code: 0,
    message: '获取用户成功',
    result: data,
  })
})

// 添加用户信息
router.post('/', async function (req, res) {
  // console.log(req.body);
  const { user_name, user_password, user_type, user_onlinestatus, user_other } =
    req.body
  //操作数据库
  let sql = `insert into gg_user (user_name,user_password,user_type,user_onlinestatus,user_other) values('${user_name}','${user_password}','${user_type}','${user_onlinestatus}','${user_other}')`
  //执行sql返回结果
  const data = await exec(sql)
  res.send({
    code: 0,
    message: '添加用户成功',
    result: {
      id: data.inserId,
      user_name,
      user_password,
      user_type,
      user_onlinestatus,
      user_other,
    },
  })
})
// 更新用户
router.put('/:user_id', async function (req, res, next) {
  const { user_id } = req.params
  const { user_name, user_password, user_type, user_onlinestatus } = req.body
  //操作数据库
  let sql = `update gg_user set user_name='${user_name}',user_password='${user_password}',user_onlinestatus='${user_onlinestatus}' where user_id= ${user_id}`
  //执行sql返回结果
  // console.log(sql)
  const data = await exec(sql)

  res.send({
    code: 0,
    message: '修改用户成功',
    result: {
      user_id,
      user_name,
      user_password,
      user_type,
    },
  })
})
router.delete('/:user_id', async function (req, res, next) {
  const { user_id } = req.params
  //操作数据库
  let sql = `delete from gg_user where user_id = ${user_id}`
  //执行sql返回结果
  const data = await exec(sql)
  res.send({
    code: 0,
    message: '删除用户成功',
  })
})

/* 用户登录
 * POST /login
 */
router.post('/login', async (req, res, next) => {
  const { user_name, user_password } = req.body
  let sql = `select * from gg_user where user_name='${user_name}' and user_password='${user_password}'`
  const data = await getOne(sql)
  if (data) {
    // 修改用户登录状态为在线
    sql = `UPDATE gg_user SET user_onlinestatus= 1 WHERE user_id=${data.user_id}`

    await exec(sql)
    res.send({
      code: 0,
      message: '登录成功',
      // 这里返回的数据是没有修改的，如果有问题再完善逻辑
      result: data,
    })
  } else {
    res.send({
      code: 1,
      message: '用户名或密码错误',
    })
  }
})

/**
 * 修改取单个用户密码
 * PUT/users/:user_name/password
 */

router.put('/:user_name/password', async function (req, res, next) {
  const { user_name, user_password } = req.params
  let sql = `update gg_user set user_name='${user_name}',user_password='${user_password}'`
  const data = await exec(sql)

  res.send({
    code: 0,
    message: '修改用户密码成功',
    result: {
      user_name,
      user_password,
    },
  })
})

// 4. 导出路由对象
module.exports = router
